<template>
  <!-- <div class='container'>标签管理</div> -->
  <div class='container'>
    <el-card class="box-card">
      <el-row type="flex">
        <el-col :span="24">
          <el-form ref="FormBD" :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="目录名称">
            <el-input v-model="formInline.tagName"></el-input>
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="formInline.state" placeholder="请选择">
              <el-option label="启用" value="1"></el-option>
              <el-option label="禁用" value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button @click="purge">清除</el-button>
            <el-button type="primary" @click="SearchTags">搜索</el-button>
          </el-form-item>
          <el-button style="float: right" type="success" icon="el-icon-edit" @click="addTags = true">新增标签</el-button>
          <!-- 返回学科 -->
        <el-button type="text" icon="el-icon-back"  v-if="this.$route.query.id" style="float: right; margin-right: 10px;" @click="$router.back()">返回学科</el-button>
          </el-form>
          </el-col>
      </el-row>
        <el-alert
          :title= sum
          type="info"
          show-icon
          style="margin: 10px 0 5px;"
          :closable="false">
        </el-alert>
            <!-- 表格 -->
        <el-table :data="subjects">
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="所属学科" prop="subjectName"></el-table-column>
        <el-table-column label="标签名称" prop="tagName"></el-table-column>
        <el-table-column label="创建者" prop="username"></el-table-column>
        <el-table-column label="创建日期" prop="addDate">
          <template slot-scope="scope">
              {{ scope.row.addDate.substring(0, scope.row.addDate.indexOf('.')).replace('T', ' ') }}
            </template>
        </el-table-column>
        <el-table-column prop="state" label="状态">
          <template slot-scope="scope">
            {{ scope.row.state === 1 ? '已启用'  : '已禁用' }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button  ref="ETable" type="text" size="small" @click="changeState(scope.row)">
                  {{ scope.row.state === 1 ? '禁用'  : '启用' }}
              </el-button>
              <el-button type="text" size="small" :disabled="scope.row.state === 1" @click="revise(scope.row)">修改</el-button>
              <el-button type="text" size="small" :disabled="scope.row.state === 1" @click="clickDialog(scope.row.id)" >删除</el-button>
              <el-dialog
                title="提示"
                :visible.sync="dialogVisible"
                width="30%"
                >
                <span><e class="el-icon-warning"></e>此操作将永久删除该标签,是否继续?</span>
                <span slot="footer" class="dialog-footer">
                  <el-button @click="dialogVisible = false">取 消</el-button>
                  <el-button type="primary" @click="remDiscipline(scope.row)">确 定</el-button>
                </span>
              </el-dialog>
            </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
      class="pager"
      background
      layout="prev, pager, next, sizes, jumper"
      :total="total"
      :page-size="reqParams.pagesize"
      :current-page="reqParams.page"
      @current-change="changePager"
      @size-change="changeSize"
      :page-sizes="[5,10,20,50]"
      ></el-pagination>
      <!-- /分页 -->
    </el-card>
    <!-- 新增弹窗 -->
    <el-dialog
        title="新增目录"
        :visible.sync="addTags"
        width="30%">
        <el-form :model="ruleForm" :rules="rules" ref="form" label-width="100px">
          <el-form-item label="所属学科" prop="subjectID">
            <el-select v-model="ruleForm.subjectID" placeholder="请选择">
              <el-option v-for="item in esList" :key="item.id" :label="item.subjectName" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="目录名称" prop="tagName">
            <el-input v-model="ruleForm.tagName" placeholder="请输入目录名称"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="Cancel">取 消</el-button>
          <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
      </el-dialog>
      <!--/ 新增弹窗 -->

      <!-- 修改弹窗 -->
      <el-dialog
        title="修改目录"
        :visible.sync="showRevise"
        width="30%">
        <el-form :model="ReviseLabel" :rules="rules" ref="form" label-width="100px">
          <el-form-item label="所属学科" prop="subjectID">
            <el-select v-model="ReviseLabel.subjectID" placeholder="请选择">
              <el-option v-for="item in esList" :key="item.id" :label="item.subjectName" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="目录名称" prop="tagName">
            <el-input v-model="ReviseLabel.tagName" placeholder="请输入目录名称"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="showRevise = false">取 消</el-button>
          <el-button type="primary" @click="modifyLabel">确 定</el-button>
        </span>
      </el-dialog>
      <!--/ 修改弹窗 -->
  </div>
</template>

<script>
import { list as getTagsList, remove, changeState, add, update } from '../../api/hmmm/tags'
import { list as getSubjectsList } from '@/api/hmmm/subjects'
export default {
  name: 'Tags',
  data () {
    return {
      formInline: {
        tagName: '',
        state: null
      },
      sum: '', // 一共多少条数据
      subjects: [],
      dialogVisible: false, // 删除弹框
      addTags: false, // 控制新增弹窗
      showRevise: false, // 控制修改弹窗
      reqParams: {
        subjectName: null,
        page: 1,
        pagesize: 10
      },
      total: 0,
      esList: [], // 学科列表数据
      ruleForm: {
        id: null,
        subjectID: '',
        tagName: ''
      },
      ReviseLabel: {
        id: '',
        tagName: '',
        subjectID: ''
      },
      // 校验规则
      rules: {
        tagName: [
          { required: true, message: '请输入标签名称', trigger: 'blur' }
        ],
        subjectID: [
          { required: true, message: '请选择所属学科', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.list()
    this.getSubjectName()
  },
  methods: {
    // 分页方法
    changePager (newPage) {
      this.reqParams.page = newPage
      this.list()
    },
    // 改变页码
    changeSize (newSize) {
      this.reqParams.page = 1
      this.reqParams.pagesize = newSize
      this.list()
    },
    // 获取学科名称
    async getSubjectName () {
      const res = await getSubjectsList()
      const data = res.data.items
      console.log(data)
      this.esList = data
    },
    // 获取标签信息
    async list () {
      if (this.$route.query.id) {
        this.reqParams.subjectID = this.$route.query.id
      } else {
        delete this.reqParams.subjectID
      }
      const ret = await getTagsList(this.reqParams)
      // console.log(111, ret)
      this.subjects = ret.data.items
      this.total = ret.data.counts
      this.sum = `一共${ret.data.counts}条数据`
    },
    // 搜索标签
    purge () {
      this.formInline.tagName = ''
      this.formInline.state = null
    },
    async SearchTags () {
      try {
        const ret = await getTagsList(this.$refs.FormBD._props.model)
        this.subjects = ret.data.items
      } catch (error) {
        console.log('error2', error)
      }
    },
    // 新增标签
    async submitForm () {
      try {
        // 校验
        const ret = await this.$refs.form.validate()
        if (!ret) return
        // 提交
        await add(this.ruleForm)
        // 提示
        this.$message.success('新增成功')
        // 更新
        this.list()
        // 关闭
        this.ruleForm.tagName = ''
        this.ruleForm.subjectID = ''
        this.addTags = false
      } catch (error) {
        this.$message.error('新增失败')
      }
    },
    // 取消删除
    Cancel () {
      this.ruleForm.tagName = ''
      this.ruleForm.subjectID = ''
      this.addTags = false
    },
    // 删除标签信息
    clickDialog (id) {
      this.dialogVisible = true
      this.ruleForm.id = id
      console.log('id', this.ruleForm.id)
    },
    async remDiscipline (data) {
      try {
        const ret = await remove(this.ruleForm)
        console.log('ret', ret)
        this.list()
        this.dialogVisible = false
        // 给用户提示删除成功
        this.$message.success('删除数据成功')
      } catch (error) {
        console.log('1111', error)
      }
    },
    // 禁用启用
    async changeState (data) {
      try {
        if (data.state === 1) {
          data.state = 0
        } else if (data.state === 0) {
          data.state = 1
        }
        const ret = await changeState(data)
        console.log('ret', ret)
        this.list()
        this.$message.success('状态更改成功')
      } catch (error) {
        console.log('error:', error)
      }
    },
    // 修改状态
    revise (data) {
      this.showRevise = true
      this.ReviseLabel.tagName = data.tagName
      this.ReviseLabel.subjectID = data.subjectID
      this.ReviseLabel.id = data.id
    },
    async modifyLabel () {
      try {
        // 校验
        const ret = await this.$refs.form.validate()
        if (!ret) return
        await update(this.ReviseLabel)
        this.list()
        this.$message.success('修改成功')
        this.showRevise = false
      } catch (error) {
        this.$message.error('修改失败')
      }
    }
  }
}
</script>

<style scoped lang='scss'>
.fenYe {
  float: right;
  margin: 20px;
}
.pager {
  margin: 20px;
  float: right;
}
 .el-icon-warning {
  font-size: 23px;
  color: #e6a23c;
  margin-right: 10px;
  padding-bottom: -5px;
}
</style>
